// pages/new_inter/house_book/house_book.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '2019-09-01',
    array: ['微信支付', '蓝旗币'],
    index: 0,
    num: '2',
    hostUrl: app.globalData.hostUrl,
    isAgree: true,
    payway:2,
    hiddenAlert: true,

  },
  // 时间选择器
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  // 支付选择器
  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var index = e.detail.value
    if (index == 0) {
      var payway=2;
    }else{
      var payway = 4;

    }
    this.setData({
      index: e.detail.value,
      payway: payway//支付方式
    })
    
  },
  // 
  bindAgreeChange: function(e) {
    this.setData({
      isAgree: !!e.detail.value.length
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.booking(options.id);
    this.setData({
      id: options.id
    })
    this.getuser();

  },
  //获取用户积分
  getuser: function () {
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'User/getUser',
      data: {
        // opt: 'getCartList',
        userId: wx.getStorageSync('userId'),
      },
      success: function (res) {
        if (res.data != null) {
          that.setData({
            userscores: res.data.Scores,
            usercommission: res.data.Commission,
          })
        }
      }
    })
  },
  //地产详情详情
  booking: function (id) {
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'House/booking',
      data: {
        // opt: 'getBanner',
        id: id,

      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res)
        if (res.data != null) {
          // var desc = that.delHtmlTag(res.data['article']['content'])
          // res.data['article']['content'] = desc;
            that.setData({
              info: res.data,
             
            })

        }
      }
    })
  },
  delHtmlTag: function (msg) {
    var msg = msg.replace(/<\/?[^>]*>/g, ''); //去除HTML Tag
    // msg = msg.replace(/[|]*\n/, '') //去除行尾空格
    msg = msg.replace(/&nbsp;/ig, ''); //去掉npsp
    return msg;
  },
  // 确定事件
  qdClick: function () {
    this.setData({
      hiddenAlert: !this.data.hiddenAlert
    })
  },
  //提交
  formSubmit: function (e) {
    // console.log(e)

    var that = this;
    var formData = e.detail.value;
    formData['isRecord'] = 0;
    formData['memberId'] = wx.getStorageSync('userId');
    formData['PayWay'] = that.data.payway;
    formData['id'] = that.data.id;

    // if (formData['Code'] == "" || formData['PassWord_2'] == "" || formData['yanzhengma'] == "" || formData['RefereeCode'] == "") {
    //   wx.showToast({
    //     title: '必填',
    //     icon: 'none'
    //   })
    //   return;
    // }
    // that.setData({
    //   button: 'false'
    // })
    // console.log(that.data.button)
    // return ;
    if (that.data.payway == 4) {
      console.log(111, '蓝旗币计算')
      if (that.data.info['info'].Price > that.data.userscores) {
        wx.showToast({
          title: '蓝旗币不足!',
          icon: 'none',
          duration: 1500
        })
        return;
      }
    }
    wx.request({
      url: app.globalData.apiUrl + 'House/createOrder',
      data: formData,
      header: {
        'Content-Type': 'application/json'
      },
      method: 'POST',
      success: function (res) {
        console.log(res)
        if (res.data.status == 1) {
          
          if (formData['PayWay'] == 2) {
            that.pay(res.data.id, formData['PayWay']);

          } else {
            wx.navigateTo({
              url: '../zhifu/zhifu?orderid=' + res.data.id + '&type=' + res.data.message,
            })
          }
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
        // that.setData({
        //   button: 'true'
        // })
      }
    })
  },
  //微信支付
  pay: function (id, payway) {//支付
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'Pay/kanfang_GetUnifiedOrderResult',
      data: {
        // opt: 'GetUnifiedOrderResult',
        userId: wx.getStorageSync('userId'),
        id: id,//订单id
        // typeId: 2,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res, '支付返回信息')
        // return ;
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': res.data.package,
          'signType': 'MD5',
          'paySign': res.data.paySign,
          'success': function (res) {
            console.log(res)
            wx.navigateTo({
              url: '../../personal/yuyue/yuyue',
            })
          },
          'fail': function (res) {
            wx.navigateTo({
              url: '../../personal/yuyue/yuyue',
            })
          },
          'complete': function (res) {
            wx.navigateTo({
              url: '../../personal/yuyue/yuyue',
            })
          }
        })

      }
    })
  },
  onReady: function() {
    this.alertview = this.selectComponent('#alertview')
  },
  // 按钮点击事件
  clickMe: function() {
    this.setData({
      hiddenAlert: !this.data.hiddenAlert
    })
  },
  // 确定事件
  confirmEvent: function() {
    this.alertview.hideAlertView();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  index: function () {
    wx.reLaunch({
      url: '/pages/index/index'
    })
  },


})